using UnityEngine;
using System.Collections;

public class DemoRotate : MonoBehaviour {

    private float i = 1;
    private float amount = 2f;
    public bool ready;
    private state currentState = state.UPPER;
    private float u;
    private float aAmount = 0.4f;
    float current = 0.4f;

	void Update () 
    {
        if (ready)
        {
            i -= amount * Time.deltaTime;

            if (i < 0)
                Destroy(gameObject);

            renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, i);
            transform.position = new Vector3(transform.position.x, transform.position.y, -2);
            transform.localScale = new Vector3(transform.localScale.x + 1 - i, transform.localScale.y + 1 - i, transform.localScale.z);
        }
        else
        {
            if (u > 0.1f)
            {

                u = 0;

                switch (currentState)
                {
                    case state.UPPER:
                        currentState = state.DOWN;
                        current = transform.localScale.x;
                        break;

                    case state.DOWN:
                        currentState = state.UPPER;
                        current = transform.localScale.x;
                        break;
                }
            }
            else
            {
                u += Time.deltaTime * aAmount;               

                if (currentState == state.UPPER)
                {
                    transform.localScale = new Vector3(current + u, current + u, transform.localScale.z);
                }
                else
                {
                    transform.localScale = new Vector3(current - u, current - u, transform.localScale.z);
                }
            }
        }
	}

    private enum state
    { 
        UPPER,
        DOWN
    }
}
